package com.wejoy.video.middle.station.data.service.workorder.impl;

import com.wejoy.video.middle.station.data.domain.workorder.OrderPhotoLib;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.workorder.IOrderPhotoLibMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.workorder.IOrderPhotoLibService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工单图片资源
 *
 * @author miracle_j
 * @date 2023年01月03日 10:23:56
 */
@Service
public class OrderPhotoLibServiceImpl implements IOrderPhotoLibService {
    @Autowired
    private IOrderPhotoLibMapper orderPhotoLibMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(OrderPhotoLib req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(OrderPhotoLib req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return orderPhotoLibMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(OrderPhotoLib orderPhotoLib) {
        WJAssert.notNull(orderPhotoLib, "入参对象不能为空");
        WJAssert.notNull(orderPhotoLib.getId(), "id参数错误");
        OrderPhotoLib where = new OrderPhotoLib();
        where.setId(orderPhotoLib.getId());
        return orderPhotoLibMapper.updateSelective(orderPhotoLib, where);
    }

    @Override
    public int getCount(OrderPhotoLib orderPhotoLib) {
        checkStringLength(orderPhotoLib, true);
        return orderPhotoLibMapper.getCount(orderPhotoLib);
    }

    @Override
    public OrderPhotoLib getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        OrderPhotoLib where = new OrderPhotoLib();
        where.setId(id);
        return orderPhotoLibMapper.getOne(where);
    }

    @Override
    public List<OrderPhotoLib> getList(OrderPhotoLib orderPhotoLib) {
        checkStringLength(orderPhotoLib, true);
        return orderPhotoLibMapper.getList(orderPhotoLib);
    }

    @Override
    public List<OrderPhotoLib> getPageList(OrderPhotoLib orderPhotoLib) {
        checkStringLength(orderPhotoLib, true);
        return orderPhotoLibMapper.getPageList(orderPhotoLib);
    }

    @Override
    public void audit(Long id, String operator) {
        OrderPhotoLib req = new OrderPhotoLib();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        OrderPhotoLib req = new OrderPhotoLib();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        OrderPhotoLib req = new OrderPhotoLib();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(OrderPhotoLib orderPhotoLib, boolean nullAble) {
        WJAssert.notNull(orderPhotoLib, "入参对象不能为空");
        WJAssert.limitMaxLength(orderPhotoLib.getImageUrl(), true, 255, "工单图像地址不合法!");
        WJAssert.limitMaxLength(orderPhotoLib.getCallBackStatus(), true, 20, "回调时状态不合法!");
        WJAssert.limitMaxLength(orderPhotoLib.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(orderPhotoLib.getRemark(), true, 255, "备注不合法!");
    }
}
